package com.tapmobi.xrtb;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gridmobi.xrtb.common.HttpClientUtil;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;

public class AppInfo {
    static AtomicInteger errorCnt = new AtomicInteger(0);
    public static void main(String[] args)  throws  Exception{

       /* String[] apps = {"622434129",
                "1193508329",
                "418987775",
                "1324604053",
                "382617920",
                "587366035",
                "1274972321",
                "1337578317",
                "462678375",
                "355554941",
                "657500465",
                "429610587",
                "314498713",
                "1097815000",
                "597928938",
                "302324249",
                "749083919",
                "372648912",
                "1190849728",
                "749124884",
                "1444418347",
                "448999087",
                "439873467",
                "1089336971",
                "400169658",
                "1236833677",
                "1055672607",
                "591560124",
                "1064910141",
                "600012822",
                "1302018227",
                "319881193",
                "1093108529",
                "545551605",
                "526783584",
                "1425917097",
                "539124565",
                "606413051",
                "384041016",
                "1232693016",
                "387301602",
                "466970942",
                "1069361548",
                "349812998",
                "286756410",
                "724816878",
                "1096825045",
                "1276695139",
                "645704840",
                "639247498",
                "432866707",
                "1025462912",
                "1196764367",
                "1057968965",
                "350426804",
                "1031653653",
                "1413942319",
                "338088432",
                "341232718",
                "1313211434",
                "1017261655",
                "635896473",
                "749046891",
                "292987597",
                "1280476285",
                "804641004",
                "317374984",
                "909351158",
                "979401801",
                "432450349",
                "1135368655",
                "404386888",
                "643791032",
                "973134481",
                "475976577",
                "1073082439",
                "1235581326",
                "1266303977",
                "1383248144",
                "306937222",
                "380098393",
                "510873505",
                "558485480",
                "573166436",
                "911793120",
                "1023782170",
                "1154169098",
                "1163786766",
                "1267331464",
                "1273749004",
                "1330905303",
                "315241195",
                "398383945",
                "414569920",
                "416345319",
                "420447115",
                "424216726",
                "523001403",
                "564540143",
                "652945710",
                "912390687",
                "1407852246",
                "387675255",
                "593715088",
                "1206761632",
                "387816411",
                "1355464544",
                "375242617",
                "1407686794",
                "1436275279",
                "1207472156",
                "966709467",
                "527986338",
                "733948370",
                "1425793208",
                "1420518868",
                "686420990",
                "579962583",
                "396301854",
                "1423046460",
                "822284884",
                "882313725",
                "1435120620",
                "1047649534",
                "1290119012",
                "840919914",
                "1342468799",
                "1093341512",
                "338764030",
                "567263076",
                "1085652055",
                "1139609950",
                "513095741",
                "647804489",
                "941620316",
                "1174964304",
                "1370327712",
                "1431556749",
                "1434562378",
                "551992213",
                "627672547",
                "791606913",
                "985880059",
                "1078101854",
                "1096954981",
                "1100548985",
                "1106461381",
                "1163777816",
                "1225888584",
                "1253455582",
                "1323957120",
                "1334311990",
                "1384622732",
                "1444308762",
                "1444781694",
                "417571834",
                "438875956",
                "446661328",
                "574470741",
                "642064350",
                "847859021",
                "903751102",
                "619175115",
                "990209124",
                "1381389173",
                "1126258529",
                "1187367827",
                "1077871641",
                "542329348",
                "1091238155",
                "1014924797",
                "1252520771",
                "1289134991",
                "546417608",
                "967926147",
                "397648381",
                "322439990",
                "1297924322",
                "1281474674",
                "773163173",
                "399355755",
                "600780511",
                "1034709839",
                "1333390435",
                "596328120",
                "1058774427",
                "559788663"};*/

        String[] apps = {"733948370",
                "1252520771",
                "1337578317",
                "1096954981",
                "1355464544",
                "1281474674",
                "657500465",
                "1089336971",
                "1444418347",
                "591560124",
                "587366035",
                "635896473",
                "1093108529",
                "1206761632",
                "400169658",
                "1078101854",
                "1274972321",
                "990209124",
                "1407852246",
                "1232693016",
                "967926147",
                "579962583",
                "1324604053",
                "749083919",
                "466970942",
                "1097815000",
                "1330905303",
                "1017261655",
                "822284884",
                "596328120",
                "1193508329",
                "1302018227",
                "1057968965",
                "1190849728",
                "526783584",
                "1236833677",
                "545551605",
                "593715088",
                "909351158",
                "1069361548",
                "749124884",
                "1055672607",
                "429610587",
                "397648381",
                "1025462912",
                "645704840",
                "749046891",
                "966709467",
                "1266303977",
                "398383945",
                "355554941",
                "559788663",
                "1273749004",
                "387301602",
                "804641004",
                "564540143",
                "724816878",
                "1425917097",
                "420447115",
                "652945710",
                "292987597",
                "1436275279",
                "1225888584",
                "1267331464",
                "574470741",
                "439873467",
                "903751102",
                "1154169098",
                "338088432",
                "1058774427",
                "417571834",
                "847859021",
                "462678375",
                "1333390435",
                "1420518868",
                "1253455582",
                "315241195",
                "375242617",
                "1297924322",
                "546417608",
                "1334311990",
                "912390687",
                "416345319",
                "510873505",
                "387675255",
                "399355755",
                "1444308762",
                "1100548985",
                "539124565",
                "446661328",
                "306937222",
                "619175115",
                "606413051",
                "1207472156",
                "1135368655",
                "622434129",
                "979401801",
                "1077871641",
                "1413942319",
                "1425793208",
                "1096825045",
                "475976577",
                "542329348",
                "1423046460",
                "314498713",
                "1313211434",
                "1342468799",
                "558485480",
                "1031653653",
                "1444781694",
                "642064350",
                "973134481",
                "1023782170",
                "1384622732",
                "1435120620",
                "319881193",
                "404386888",
                "1014924797",
                "1163777816",
                "1187367827",
                "1370327712",
                "322439990",
                "414569920",
                "424216726",
                "438875956",
                "1034709839",
                "1047649534",
                "1073082439",
                "1085652055",
                "1091238155",
                "1093341512",
                "1106461381",
                "1126258529",
                "1139609950",
                "1163786766",
                "1174964304",
                "1196764367",
                "1235581326",
                "1276695139",
                "1289134991",
                "1290119012",
                "1381389173",
                "1407686794",
                "1431556749",
                "1434562378",
                "286756410",
                "302324249",
                "317374984",
                "338764030",
                "341232718",
                "349812998",
                "350426804",
                "372648912",
                "380098393",
                "382617920",
                "384041016",
                "387816411",
                "396301854",
                "418987775",
                "432450349",
                "432866707",
                "513095741",
                "567263076",
                "573166436",
                "597928938",
                "600012822",
                "600780511",
                "627672547",
                "639247498",
                "643791032",
                "647804489",
                "686420990",
                "773163173",
                "791606913",
                "840919914",
                "882313725",
                "911793120",
                "941620316",
                "985880059"};
        for (String id : apps) {
            String appurl = "https://api.qimai.cn/app/appinfo?analysis=fWR5AX1aZUEpc1tXeWdcDXATH1FAQBcFR0gNWlAOcBMDBQkFAABRBgFSAHZCAQ%3D%3D&appid=" + id;
            String baseurl = "https://api.qimai.cn/app/baseinfo?analysis=fWR5AX1aZUEpc1tXeWdcDXATH1FAQBcGVksBXVgHX3ATAwUJBQhcAgldAgAhEwE%3D&appid=" + id;
            try {
                JSONObject appJsonn = HttpClientUtil.getAndBodyAsJson(appurl);
                JSONObject baseJson = HttpClientUtil.getAndBodyAsJson(baseurl);


                String appname = "";
                String appstore_url = "";
                String price = "";
                String publisher = "";
                String offline = "";
                String cat = "";
                String catNo = "";
                if (baseJson.get("msg").equals("成功")) {
                    JSONArray base = baseJson.getJSONArray("appInfo");
                    if (appJsonn != null && appJsonn.getJSONObject("appInfo") != null) {
                        JSONObject appinfo = appJsonn.getJSONObject("appInfo");

                        appname = appinfo.getString("appname");
                        appstore_url = appinfo.getString("appstore_url");
                        price = appinfo.getString("price");
                        publisher = appinfo.getString("publisher");
                        offline = appinfo.getString("offline");
                        if (appinfo.containsKey("genre")) {
                            cat = appinfo.getJSONObject("genre").getString("name");
                            catNo = appinfo.getJSONObject("genre").getString("param");

                        }

                    }
                    String s1 = appJsonn.getJSONObject("appInfo").get("appid") + "#" + baseJson.get("description");
                    String s2 = s1.replace("\n", "").replace("<br />", "");

                    System.out.println(id + "^" + appname + "^" + publisher + "^" + offline + "^" + cat + "^" + catNo + "^" + price + "^" + getV("开发商", base) + "^" + getV("发布日期", base) + "^" + getV("更新日期", base) + "^" + getV("版本", base) + "^" + getV("大小", base) + "^" + getV("兼容性", base) + "^" + appstore_url + "^" + s2);

                }
            } catch (IOException e) {
                errorCnt.incrementAndGet();
            }
        }
        System.out.println("errors:"+errorCnt.get());

    }

    public static String getV(String key, JSONArray vs) {
        String v = "";
        if (vs != null) {
            for (Object n : vs) {
                JSONObject obj = (JSONObject) n;
                if ((obj.containsKey("name") && obj.getString("name").equals(key))) {
                    if (obj.containsKey("value")) {
                        v = obj.getString("value");
                    }
                }
            }

        }
        return v;
    }


}